﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;
using HtmlAgilityPack;

namespace HtmlHelper
{
    public static class HtmlNode
    {
        /// <summary>
        /// 获取html的属性
        /// </summary>
        /// <param name="htmlNode"></param>
        /// <param name="str"></param>
        public static NodeAttribute GetNodeAttribute(this HtmlAgilityPack.HtmlNode htmlNode, string str)
        {
            var attr = new NodeAttribute()
            {
                Value = htmlNode.Attributes[str].Value,
                HtmlNode = htmlNode.Attributes[str].OwnerNode,
                XPath = htmlNode.Attributes[str].XPath,
                Name = htmlNode.Attributes[str].Name,
                OriginalName = htmlNode.Attributes[str].OriginalName,
                Line = htmlNode.Attributes[str].Line
            };
            return attr;
        }
        /// <summary>
        /// 节点里的文本
        /// </summary>
        /// <param name="htmlNode"></param>
        /// <returns></returns>
        public static string InnerText(this HtmlAgilityPack.HtmlNode htmlNode)
        {
            return htmlNode.InnerText;
        }
        /// <summary>
        /// 节点里的HTML
        /// </summary>
        /// <param name="htmlNode"></param>
        /// <returns></returns>
        public static string InnerHtml(this HtmlAgilityPack.HtmlNode htmlNode)
        {
            return htmlNode.InnerHtml;
        }
        /// <summary>
        /// 子节点
        /// </summary>
        /// <param name="htmlNode"></param>
        /// <returns></returns>
        public static HtmlNodeCollection ChildNodes(this HtmlAgilityPack.HtmlNode htmlNode)
        {
           return htmlNode.ChildNodes;
        }
        /// <summary>
        /// 第一个子节点
        /// </summary>
        /// <param name="htmlNode"></param>
        /// <returns></returns>
        public static HtmlAgilityPack.HtmlNode FirstChild(this HtmlAgilityPack.HtmlNode htmlNode)
        {
            return htmlNode.FirstChild;
        }
        /// <summary>
        /// 最后的子节点
        /// </summary>
        /// <param name="htmlNode"></param>
        /// <returns></returns>
        public static HtmlAgilityPack.HtmlNode LastChild(this HtmlAgilityPack.HtmlNode htmlNode)
        {
            return htmlNode.LastChild;
        }
        /// <summary>
        /// 父节点
        /// </summary>
        /// <param name="htmlNode"></param>
        /// <returns></returns>
        public static HtmlAgilityPack.HtmlNode ParentNode(this HtmlAgilityPack.HtmlNode htmlNode)
        {
            return htmlNode.ParentNode;
        }
        /// <summary>
        /// 节点类型
        /// </summary>
        /// <param name="htmlNode"></param>
        /// <returns></returns>
        public static HtmlNodeType NodeType(this HtmlAgilityPack.HtmlNode htmlNode)
        {
            return htmlNode.NodeType;
        }
    }
}
